<?php
/**
 * Created by PhpStorm.
 * User: chuyun
 * Date: 2018/8/15
 * Time: 17:20
 */

namespace App\Controllers;


use App\Models\Access;
use Interop\Container\ContainerInterface;
use Slim\Http\Request;

class AccessController extends BaseController
{
    /**
     * @var \Illuminate\Database\Capsule\Manager
     */
    protected  $DB;
    public function __construct(ContainerInterface $container)
    {
        parent::__construct($container);
        $this->DB = $this->container->db;
    }

    public function getRoleNodelist(Request $request) {
        $params = $request->getParsedBody();
        if(intval($params['role_id'])==0){
            return $this->json(1005,'id不合法');
        }
        $list = Access::where('role_id',$params['role_id'])->get();
        $data = [];
        foreach ($list as $key=>$val){
            array_push($data,$val['node_id']);
        }
        return $this->jsonWithData(1,'OK',$data);
    }

    public function roleNodeStore(Request $request) {
        $params = $request->getParsedBody();
        if(intval($params['role_id'])==0){
            return $this->json(1005,'id不合法');
        }
        Access::where('role_id',$params['role_id'])->delete();
        if(isset($params['nodes']) && !empty($params['nodes'])){
            $accesss = [];
            foreach ($params['nodes'] as $key => $val) {
                $tmp = [];
                $tmp['role_id']    = $params['role_id'];
                $tmp['node_id']    = $val;
                $tmp['created_at'] = date('Y-m-d H:i:s');
                $tmp['updated_at'] = date('Y-m-d H:i:s');
                array_push($accesss,$tmp);
            }
            $ret = Access::insert($accesss);
            if($ret){
                return $this->json(1,'OK');
            }else{
                return $this->json(1,'权限分配失败');
            }
        }
    }

    public function getNavlist(Request $request) {
        $token = $this->getToken($request);

        $navList = $this->DB::table('role_user_group')
                ->join('user','role_user_group.group_id','=','user.group_id')
                ->join('access','access.role_id','=','role_user_group.role_id')
                ->join('node','node.id','=','access.node_id')
                ->where(['token'=>$token,'node.level'=>'1','pid'=>0])
                ->select('node.id','node.name','node.title','node.url','node.pid')
                ->get();
        $data = [];
        foreach ($navList as $key=>$val){
            $data[$key]['id']       = $val->id;
            $data[$key]['name']     = $val->name;
            $data[$key]['title']    = $val->title;
            $data[$key]['url']      = $val->url;
            $data[$key]['pid']      = $val->pid;
            $data[$key]['children'] = $this->getNavChild($token,$val->id);
        }
        return $this->jsonWithData(1,'OK',$data);
    }

    public function getBtnlist(Request $request){
        $token = $this->getToken($request);

        $navList = $this->DB::table('role_user_group')
            ->join('user','role_user_group.group_id','=','user.group_id')
            ->join('access','access.role_id','=','role_user_group.role_id')
            ->join('node','node.id','=','access.node_id')
            ->where(['token'=>$token,'node.level'=>'2'])
            ->select('node.id','node.name','node.title')
            ->get();
        $data = [];
        foreach ($navList as $key=>$val){
            $data[$key] = $val->name;
        }
        return $this->jsonWithData(1,'OK',$data);
    }

    protected function getNavChild($token,$pid){
        $node = $this->DB::table('role_user_group')
            ->join('user','role_user_group.group_id','=','user.group_id')
            ->join('access','access.role_id','=','role_user_group.role_id')
            ->join('node','node.id','=','access.node_id')
            ->where(['token'=>$token,'node.level'=>'1','pid' => $pid])
            ->select('node.id','node.name','node.title')
            ->get();
        $data = ['index'];
        foreach ($node as $key=>$val){
            array_push($data,$val->name);
        }
        return $data;
    }
}